home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / thesrc10.zip / COMM2.C < prev    next >
C/C++ Source or Header  |  1992-08-11  |  15KB  |  518 lines

  1. /***********************************************************************/
  2. /* COMM2.C - Commands F-J                                              */
  3. /* This file contains all commands that can be assigned to function    */
  4. /* keys or typed on the command line.                                  */
  5. /***********************************************************************/
  6. /*
  7.  * THE - The Hessling Editor. A text editor similar to VM/CMS xedit.
  8.  * Copyright (C) 1991,1992 Mark Hessling
  9.  *
  10.  * This program is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU General Public License as
  12.  * published by the Free Software Foundation; either version 2 of
  13.  * the License, or any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18.  * General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to:
  22.  *
  23.  *    The Free Software Foundation, Inc.
  24.  *    675 Mass Ave,
  25.  *    Cambridge, MA 02139 USA.
  26.  *
  27.  *
  28.  * If you make modifications to this software that you feel increases
  29.  * it usefulness for the rest of the community, please email the
  30.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  31.  * This software is going to be maintained and enhanced as deemed
  32.  * necessary by the community.
  33.  *
  34.  * Mark Hessling                     email: M.Hessling@itc.gu.edu.au
  35.  * 36 David Road                     Phone: +61 7 849 7731
  36.  * Holland Park                      Fax:   +61 7 875 7877
  37.  * QLD 4121
  38.  * Australia
  39.  */
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42.  
  43. #include "the.h"
  44.  
  45. /*#define DEBUG 1*/
  46.  
  47. /*-------------------------- external data ----------------------------*/
  48. extern LINE *next_line,*curr_line;
  49. extern VIEW_DETAILS *vd_current,*vd_first;
  50. extern char current_screen;
  51. extern SCREEN_DETAILS screen[MAX_SCREENS];        /* screen structures */
  52. extern char current_file;         /* pointer to current file */
  53. extern WINDOW *foot,*error_window;
  54. extern char error_on_screen;
  55. extern unsigned char *rec;
  56. extern unsigned short rec_len;
  57. extern unsigned char mode_insert;        /* defines insert mode toggle */
  58. extern unsigned char in_profile;    /* indicates if processing profile */
  59. extern unsigned char *tempfilename;
  60. extern unsigned short file_start;
  61. /*---------------------- function definitions -------------------------*/
  62. #ifdef PROTO
  63. void split_command(unsigned char *,unsigned char *,unsigned char *);
  64. int param_split(unsigned char *,unsigned char *[],int );
  65. long valid_target(unsigned char *);
  66. int get_row_for_focus_line(int,long,long);
  67. #else
  68. void split_command();
  69. int param_split();
  70. long valid_target();
  71. int get_row_for_focus_line();
  72. #endif
  73. #ifdef DOS
  74. char get_mode(void);
  75. #endif
  76. /*man-start*********************************************************************
  77. COMMAND
  78.      ffile - force file the current file to disk
  79.  
  80. SYNTAX
  81.      FFile  [filename]
  82.  
  83. DESCRIPTION
  84.      The FFILE command writes the current file to disk to the current
  85.      file name or to the supplied filename.
  86.      Unlike the FILE command, if the optional filename exists, this
  87.      command will overwrite the file.
  88.  
  89. COMPATIBILITY
  90.      Compatible.
  91.  
  92. DEFAULT
  93.      With no parameters, the current file is written.
  94.  
  95. SEE ALSO
  96.      file,save,ssave
  97.  
  98. STATUS
  99.      Complete
  100. **man-end**********************************************************************/
  101. #ifdef PROTO
  102. int Ffile(unsigned char *params)
  103. #else
  104. int Ffile(params)
  105. unsigned char *params;
  106. #endif
  107. /***********************************************************************/
  108. {
  109. /*--------------------------- local data ------------------------------*/
  110. /*--------------------------- processing ------------------------------*/
  111. #ifdef TRACE
  112.  trace_function("comm2.c:   Ffile");
  113. #endif
  114.  post_process_line(CURRENT_VIEW->focus_line);
  115.  if (save_file(params,YES,0L,NO) == ERROR)
  116.    {
  117. #ifdef TRACE
  118.     trace_return();
  119. #endif
  120.     return(OK);
  121.    }
  122.  free_view_memory();
  123. #ifdef TRACE
  124.  trace_return();
  125. #endif
  126.  return(QUIT);
  127. }
  128. /*man-start*********************************************************************
  129. COMMAND
  130.      file - file the current file to disk
  131.  
  132. SYNTAX
  133.      FILE  [filename]
  134.  
  135. DESCRIPTION
  136.      The FILE command writes the current file to disk to the current
  137.      file name or to the supplied filename.
  138.      Unlike the FFILE command, if the optional filename exists, this
  139.      command will not overwrite the file.
  140.  
  141. COMPATIBILITY
  142.      Compatible.
  143.  
  144. DEFAULT
  145.      With no parameters, the current file is written.
  146.  
  147. SEE ALSO
  148.      ffile,save,ssave
  149.  
  150. STATUS
  151.      Complete
  152. **man-end**********************************************************************/
  153. #ifdef PROTO
  154. int File(unsigned char *params)
  155. #else
  156. int File(params)
  157. unsigned char *params;
  158. #endif
  159. /***********************************************************************/
  160. {
  161. /*--------------------------- local data ------------------------------*/
  162. /*--------------------------- processing ------------------------------*/
  163. #ifdef TRACE
  164.  trace_function("comm2.c:   File");
  165. #endif
  166.  post_process_line(CURRENT_VIEW->focus_line);
  167. /*---------------------------------------------------------------------*/
  168. /* If we are filing the current file with the same name AND the number */
  169. /* of alterations is zero, then quit the file.                         */
  170. /*---------------------------------------------------------------------*/
  171.  if (CURRENT_FILE->save_alt == 0 && strcmp(params,"") == 0)
  172.     Quit("");
  173.  else
  174.    {
  175.     if (save_file(params,NO,0L,NO) == ERROR)
  176.       {
  177. #ifdef TRACE
  178.        trace_return();
  179. #endif
  180.        return(OK);
  181.       }
  182.     free_view_memory();
  183.    }
  184. #ifdef TRACE
  185.  trace_return();
  186. #endif
  187.  return(QUIT);
  188. }
  189. /*man-start*********************************************************************
  190. COMMAND
  191.      forward - scroll forwards a number of screens
  192.  
  193. SYNTAX
  194.      FORward [n]
  195.  
  196. DESCRIPTION
  197.      The FORWARD command scrolls the file contents forwards the number
  198.      of screens specified.
  199.  
  200. COMPATIBILITY
  201.      Compatible.
  202.  
  203. DEFAULT
  204.      With no parameters, 1 screen is scrolled.
  205.  
  206. SEE ALSO
  207.      backward,top
  208.  
  209. STATUS
  210.      Complete
  211. **man-end**********************************************************************/
  212. #ifdef PROTO
  213. int Forward(unsigned char *params)
  214. #else
  215. int Forward(params)
  216. unsigned char *params;
  217. #endif
  218. /***********************************************************************/
  219. {
  220. /*--------------------------- local data ------------------------------*/
  221. #define FOR_PARAMS  1
  222.  unsigned char *word[FOR_PARAMS+1];
  223.  char parm[FOR_PARAMS];
  224.  register int i;
  225.  unsigned short num_params;
  226.  long num_pages;
  227.  unsigned short x,y;
  228. /*--------------------------- processing ------------------------------*/
  229. #ifdef TRACE
  230.  trace_function("comm2.c:   Forward");
  231. #endif
  232.  num_params = param_split(params,word,FOR_PARAMS);
  233.  if (num_params == 0)
  234.    {
  235.     num_params = 1;
  236.     word[0] = (unsigned char *)"1";
  237.    }
  238.  if (num_params != 1)
  239.    {
  240.     display_error(1,word[1]);
  241. #ifdef TRACE
  242.     trace_return();
  243. #endif
  244.     return(OK);
  245.    }
  246.  if (strcmp(word[0],"*") == 0)
  247.    {
  248.     CURRENT_VIEW->current_line = CURRENT_FILE->number_lines+1;
  249.     post_process_line(CURRENT_VIEW->focus_line);
  250.     CURRENT_VIEW->focus_line = CURRENT_FILE->number_lines+1;
  251.     pre_process_line(CURRENT_VIEW->focus_line);
  252.     if (!in_profile)
  253.       {
  254.        getyx(CURRENT_WINDOW,y,x);
  255.        wmove(CURRENT_WINDOW,CURRENT_VIEW->current_row,x);
  256.        show_page();
  257.       }
  258. #ifdef TRACE
  259.     trace_return();
  260. #endif
  261.     return(OK);
  262.    }
  263.  if ((num_pages = atol(word[0])) == 0L)
  264.    {
  265.     display_error(4,word[0]);
  266. #ifdef TRACE
  267.     trace_return();
  268. #endif
  269.     return(OK);
  270.    }
  271.  
  272.  CURRENT_VIEW->current_line =
  273.     min(CURRENT_VIEW->current_line + (CURRENT_SCREEN.rows-1)*num_pages,
  274.         CURRENT_FILE->number_lines+1);
  275.  
  276.  post_process_line(CURRENT_VIEW->focus_line);
  277.  CURRENT_VIEW->focus_line = calculate_focus_line(CURRENT_VIEW->current_row,
  278.                                                 CURRENT_VIEW->focus_line,
  279.                                                 CURRENT_VIEW->current_line);
  280. /* CURRENT_VIEW->focus_line =*/
  281. /*    min(CURRENT_VIEW->focus_line + (CURRENT_SCREEN.rows-1)*num_pages,*/
  282. /*        CURRENT_FILE->number_lines+1);*/
  283.  pre_process_line(CURRENT_VIEW->focus_line);
  284.  if (in_profile)
  285.    {
  286. #ifdef TRACE
  287.     trace_return();
  288. #endif
  289.     return(OK);
  290.    }
  291.  getyx(CURRENT_WINDOW_MAIN,y,x);
  292.  show_page();
  293.  if (CURRENT_VIEW->current_window != WINDOW_COMMAND)
  294.    {
  295.     y = get_row_for_focus_line(CURRENT_VIEW->current_row,
  296.                                CURRENT_VIEW->focus_line,
  297.                                CURRENT_VIEW->current_line);
  298.     wmove(CURRENT_WINDOW,y,x);
  299.    }
  300.  
  301. #ifdef TRACE
  302.  trace_return();
  303. #endif
  304.  return(OK);
  305. }
  306. /*man-start*********************************************************************
  307. COMMAND
  308.      get - read another file into current file
  309.  
  310. SYNTAX
  311.      GET [fileid]
  312.  
  313. DESCRIPTION
  314.      The GET command reads a file into the current file, inserting
  315.      lines after the current line.
  316.      When no fileid is supplied the temporary file is used.
  317.  
  318. COMPATIBILITY
  319.      Does not support optional fromlines - forlines.
  320.  
  321. SEE ALSO
  322.      put,putd
  323.  
  324. STATUS
  325.      Complete
  326. **man-end**********************************************************************/
  327. #ifdef PROTO
  328. int Get(unsigned char *params)
  329. #else
  330. int Get(params)
  331. unsigned char *params;
  332. #endif
  333. /***********************************************************************/
  334. {
  335. /*--------------------------- local data ------------------------------*/
  336.  unsigned char *filename;
  337.  FILE *fp;
  338.  LINE *curr;
  339.  long old_number_lines;
  340. /*--------------------------- processing ------------------------------*/
  341. #ifdef TRACE
  342.  trace_function("comm2.c:   Get");
  343. #endif
  344.  if (strcmp(params,"") == 0)   /* no fileid supplied */
  345.     filename = tempfilename;
  346.  else
  347.     filename = params;
  348.  
  349.  post_process_line(CURRENT_VIEW->focus_line);
  350.  
  351.  if ((fp = fopen(filename,"r")) == NULL)
  352.    {
  353.     display_error(9,params);
  354. #ifdef TRACE
  355.     trace_return();
  356. #endif
  357.     return(ERROR);
  358.    }
  359.  curr = ll_find(CURRENT_FILE->first_line,CURRENT_VIEW->current_line);
  360.  if (curr->next == NULL)   /* on bottom of file */
  361.     curr = curr->prev;
  362.  old_number_lines = CURRENT_FILE->number_lines;
  363.  if ((curr = read_file(fp,curr,filename)) == NULL)
  364.    {
  365. #ifdef TRACE
  366.     trace_return();
  367. #endif
  368.     return(ERROR);
  369.    }
  370.  
  371.  fclose(fp);
  372.  pre_process_line(CURRENT_VIEW->focus_line);
  373. /*---------------------------------------------------------------------*/
  374. /* Fix the positioning of the marked block (if there is one and it is  */
  375. /* in the current view).                                               */
  376. /*---------------------------------------------------------------------*/
  377.  adjust_marked_block(YES,CURRENT_VIEW->current_line,
  378.                      CURRENT_FILE->number_lines - old_number_lines);
  379. /*---------------------------------------------------------------------*/
  380. /* Increment the number of lines counter for the current file and the  */
  381. /* number of alterations.                                              */
  382. /*---------------------------------------------------------------------*/
  383.  increment_alt();
  384.  show_page();
  385. #ifdef TRACE
  386.  trace_return();
  387. #endif
  388.  return(OK);
  389. }
  390. /*man-start*********************************************************************
  391. COMMAND
  392.      help - display help for the editor
  393.  
  394. SYNTAX
  395.      HELP
  396.  
  397. DESCRIPTION
  398.      The HELP command displays help for the editor.
  399.      Uses THE_HELP environment variable to point to the help file.
  400.  
  401. COMPATIBILITY
  402.      Should be similar.
  403.  
  404. STATUS
  405.      Complete.
  406. **man-end**********************************************************************/
  407. #ifdef PROTO
  408. int Help(unsigned char *params)
  409. #else
  410. int Help(params)
  411. unsigned char *params;
  412. #endif
  413. /***********************************************************************/
  414. {
  415. /*------------------------- external data -----------------------------*/
  416. extern unsigned char help_pathname[80];
  417. /*--------------------------- local data ------------------------------*/
  418.  unsigned char *help_var;
  419. /*--------------------------- processing ------------------------------*/
  420. #ifdef TRACE
  421.  trace_function("comm2.c:   Help");
  422. #endif
  423.  if ((help_var = (unsigned char *)getenv("THE_HELP")) == NULL)
  424.    {
  425.     if (file_exists(help_pathname))
  426.        help_var = help_pathname;
  427.     else
  428.       {
  429.        display_error(23,(unsigned char *)"");
  430. #ifdef TRACE
  431.        trace_return();
  432. #endif
  433.        return(OK);
  434.       }
  435.    }
  436.  Edit(help_var);
  437. #ifdef TRACE
  438.  trace_return();
  439. #endif
  440.  return(OK);
  441. }
  442. /*man-start*********************************************************************
  443. COMMAND
  444.      input - insert the command line contents into the file
  445.  
  446. SYNTAX
  447.      Input contents
  448.  
  449. DESCRIPTION
  450.      The INPUT command inserts the remainder of the command line into the
  451.      file aafter the current line.
  452.  
  453. COMPATIBILITY
  454.      Should be similar.
  455.  
  456. STATUS
  457.      Complete.
  458. **man-end**********************************************************************/
  459. #ifdef PROTO
  460. int Input(unsigned char *params)
  461. #else
  462. int Input(params)
  463. unsigned char *params;
  464. #endif
  465. /***********************************************************************/
  466. {
  467. /*--------------------------- local data ------------------------------*/
  468.  LINE *curr;
  469.  unsigned long li;
  470. /*--------------------------- processing ------------------------------*/
  471. #ifdef TRACE
  472.  trace_function("comm2.c:   Input");
  473. #endif
  474.  insert_new_line(params,strlen(params),1L,TRUE);
  475. #ifdef TRACE
  476.  trace_return();
  477. #endif
  478.  return(OK);
  479. }
  480. /*man-start*********************************************************************
  481. COMMAND
  482.      insertmode - put editor into insert mode
  483.  
  484. SYNTAX
  485.      INSERTMode
  486.  
  487. DESCRIPTION
  488.      The insertmode command toggles the insert mode within THE.
  489.  
  490. COMPATIBILITY
  491.      Compatible.
  492.  
  493. STATUS
  494.      Complete.
  495. **man-end**********************************************************************/
  496. #ifdef PROTO
  497. int Insertmode(unsigned char *params)
  498. #else
  499. int Insertmode(params)
  500. unsigned char *params;
  501. #endif
  502. /***********************************************************************/
  503. {
  504. /*--------------------------- local data ------------------------------*/
  505. /*--------------------------- processing ------------------------------*/
  506. #ifdef TRACE
  507.  trace_function("comm2.c:   Insertmode");
  508. #endif
  509.  mode_insert = (mode_insert) ? NO : YES;
  510. #if defined(DOS) || defined(OS2)
  511.  draw_cursor();
  512. #endif
  513. #ifdef TRACE
  514.  trace_return();
  515. #endif
  516.  return(OK);
  517. }
  518.